<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">{{ company.manager }}</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addDepts(company)"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <!-- 用了一个行列布局 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleClickDepts($event, data)">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          command="add"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          command="edit"
                        >修改部门</el-dropdown-item>
                        <el-dropdown-item
                          command="del"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- sync:表示父组件把传给子组件的值，放权给子组件去修改 -->
      <addDepts
        ref="addDepts"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        :depart-list="departList"
        @add-success="getDepartmentList"
      />
    </div>
  </div>
</template>

<script>
import { getDepartmentsListApi, delDepartmentsApi } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import addDepts from './components/add-dept' // 引入新增部门组件

export default {
  name: 'Departments',
  components: {
    addDepts
  },
  data() {
    return {
      // label 表示的当前节点的文本
      // children:表示的当前节点有没有子级
      company: {
        id: '',
        name: '',
        manager: '负责人',
        children: []
      },
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      nodeData: {},
      departList: [],
      isLoading: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.isLoading = true
      const { data } = await getDepartmentsListApi()
      this.company.name = data.companyName
      this.departList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      this.company.children = this.departs
      this.isLoading = false
      // console.log(data)
    },
    handleClickDepts(cmd, data) {
      if (cmd === 'add') {
        // 点击的添加按钮
        // console.log('点击添加按钮')
        // console.log(cmd)
        // console.log(data)
        this.addDepts(data)
      }
      if (cmd === 'edit') {
        // 点击的修改按钮
        // console.log('点击修改按钮')
        this.editDepts(data)
      }
      if (cmd === 'del') {
        // 点击的删除按钮
        // console.log('点击删除按钮')
        this.delDepts(data.id)
      }
    },
    // 修改
    editDepts(data) {
      this.showDialog = true
      this.nodeData = data
      // this.$refs.addDepts.getUserSimpleList()
      // this.$refs.addDepts.showTitle = '编辑部门'
      this.$nextTick(() => {
        // 说明 异步DOM更新完毕了
        this.$refs.addDepts.getDepartmentDetail()
      })
    },
    /**
     * 添加功能
     */
    addDepts(data) {
      this.showDialog = true
      this.nodeData = data
      // this.$refs.addDepts.showTitle = '添加部门'
      // this.$refs.addDepts.getUserSimpleList()
    },
    closeDialog() {
      this.showDialog = false
    },
    /**
     * 删除功能
     */
    delDepts(id) {
      this.$confirm('您确定要删除吗？该操作不可逆哟', '温馨提示')
        .then(async() => {
          // 删除成功
          await delDepartmentsApi(id)
          // this.$message({
          //   message: '删除成功',
          //   type: 'success'
          // })

          // 提示成功
          this.$message.success('删除成功')
          this.getDepartmentList()
        })
        .catch((error) => {
          console.log(error)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
